//index.js
const Controllers = require('../../controllers/Controller.js').controller;
const utils = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    // formData: {
    //   name: '员工1',
    //   mobile: '13600000000'
    // },
    formData: {},
    saving: false
  },
  onLoad(option) {
  },
  onShow() {
    var that = this;
    that.setData({
      saving: false
    })
  },
  addStaff() {
    var that = this;
    var formData = that.data.formData;
    var saving = that.data.saving;
    console.log('addStaff--formData==', formData);
    
    if (!formData.name || !formData.mobile || that.data.saving) {
      return;
    }
    that.data.saving = true;
    Controllers.addStaff(formData).then(res => {
      console.log('addStaff--res==', res);
      const { code, data, desc } = res;
      wx.showToast({
        title: desc,
        icon: code === 1 ? 'success' : 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
        that.data.saving = false;
      }, 1000);
    }).catch(res => {
      that.data.saving = false;
    });
  },
  getInput(e) {
    var that = this;
    var name = e.target.dataset.name;
    var value = e.detail.value;
    var formData = that.data.formData;
    if (name === 'mobile') {
      if (value.length === 11) {
        if (!utils.checkPhone(value)) {
          wx.showToast({
            title: '手机号码格式不正确',
            icon: 'none'
          });
          return;
        } else {
          formData[name] = value;
          console.log('formData==', formData);
          
          that.setData({
            formData
          });
        }
      }
      return;
    }
    formData[name] = value;
    that.setData({
      formData
    })
    console.log('that.data.formData==', that.data.formData);
  },
})